package org.dtrd.modules.util;

import cn.hutool.core.util.NumberUtil;
import org.dtrd.config.system.Constant;
import org.dtrd.config.system.Constant.RecordStatus;
import org.dtrd.modules.record.entity.bean.DailySugarRecordBean;
import org.dtrd.modules.record.entity.bean.ValueUplowBean;
import org.dtrd.modules.record.service.IDtrdEntRdUplowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Optional;

/**
 * @Author: QR
 * @Date: 2021/9/28-16:49
 */
@SuppressWarnings("unused")
@Component
public class SchemeUtil {

    private static IDtrdEntRdUplowRecordService uplowRecordService;

    @Autowired
    public SchemeUtil(IDtrdEntRdUplowRecordService uplowRecordService) {
        SchemeUtil.uplowRecordService = uplowRecordService;
    }

    public static List<DailySugarRecordBean> checkSugarStatus(Integer patientId, List<DailySugarRecordBean> list) {
        ValueUplowBean upLow = SchemeUtil.getUpLow(patientId);
        if (upLow != null) {
            for (DailySugarRecordBean bean : list) {
                // 空腹
                Optional.ofNullable(bean.getBeforeBreakfast())
                        .ifPresent(bb -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueLimosis(), upLow.getLowerValueLimosis(), bb);
                            bean.setBeforeBreakfastStatus(result);
                        });
                // 早餐前
                Optional.ofNullable(bean.getAfterBreakfast())
                        .ifPresent(ab -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueAfterMeal(), upLow.getLowerValueAfterMeal(), ab);
                            bean.setAfterBreakfastStatus(result);
                        });
                // 午餐前
                Optional.ofNullable(bean.getBeforeLunch())
                        .ifPresent(bl -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueBeforeMeal(), upLow.getLowerValueBeforeMeal(), bl);
                            bean.setBeforeLunchStatus(result);
                        });
                // 午餐后
                Optional.ofNullable(bean.getAfterLunch())
                        .ifPresent(al -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueAfterMeal(), upLow.getLowerValueAfterMeal(), al);
                            bean.setAfterLunchStatus(result);
                        });
                // 晚餐前
                Optional.ofNullable(bean.getBeforeDinner())
                        .ifPresent(bd -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueBeforeMeal(), upLow.getLowerValueBeforeMeal(), bd);
                            bean.setBeforeDinnerStatus(result);
                        });
                // 晚餐后
                Optional.ofNullable(bean.getAfterDinner())
                        .ifPresent(ad -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueAfterMeal(), upLow.getLowerValueAfterMeal(), ad);
                            bean.setAfterDinnerStatus(result);
                        });
                // 凌晨
                Optional.ofNullable(bean.getEarlyMorning())
                        .ifPresent(em -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueBeforeSleep(), upLow.getLowerValueBeforeSleep(), em);
                            bean.setEarlyMorningStatus(result);
                        });
                // 睡前
                Optional.ofNullable(bean.getBeforeSleep())
                        .ifPresent(bs -> {
                            Integer result = SchemeUtil.compare(upLow.getUpperValueBeforeSleep(), upLow.getLowerValueBeforeSleep(), bs);
                            bean.setBeforeSleepStatus(result);
                        });
            }
        }
        return list;
    }


    public static Integer compareBmiStatus(Float bmi) {
//        if (bmi < 18.5) {
//            return LOW.getType();
//        } else if (bmi > 23.9) {
//            return HIGH.getType();
//        } else {
//            return NORMAL.getType();
//        }
        return compare(23.9f,18.5f,bmi);
    }

    public static Float whratio(Float waist, Float hip) {
        return NumberUtil.round(waist / hip, 1).floatValue();
    }


    /**
     * 获取血糖状态
     *
     * @param sugarValue 血糖值
     * @param type       血糖时间类型
     * @param patientId  患者id
     * @return 比较结果
     */
    public static Integer compareSugarStatus(Float sugarValue, Integer type, Integer patientId) {
        ValueUplowBean upLowRecord = getUpLow(patientId);
        Constant.SugarRecordType sugarRecordType = Constant.SugarRecordType.valueOf(type);
        switch (sugarRecordType) {
            // 空腹
            case BEFOREBREAKFAST:
                return compare(upLowRecord.getUpperValueLimosis(), upLowRecord.getLowerValueLimosis(), sugarValue);
            // 餐前
            case BEFORELUNCH:
            case BEFOREDINNER:
                return compare(upLowRecord.getUpperValueBeforeMeal(), upLowRecord.getLowerValueBeforeMeal(), sugarValue);
            // 睡前
            case BEFORESLEEP:
            case EARLYMORNING:
                return compare(upLowRecord.getUpperValueBeforeSleep(), upLowRecord.getLowerValueBeforeSleep(), sugarValue);
            case AFTERBREAKFAST:
            case AFTERLUNCH:
            case AFTERDINNER:
                return compare(upLowRecord.getUpperValueAfterMeal(), upLowRecord.getLowerValueAfterMeal(), sugarValue);
            default:
                return RecordStatus.NORMAL.getType();
        }
    }

    public static ValueUplowBean getUpLow(Integer patientId) {
        return uplowRecordService.getSugarValueUpLowRecord(null, patientId);
    }

    public static Integer compare(Float up, Float low, Float value) {
        if (value < low) {
            return RecordStatus.LOWER.getType();
        } else if (value > up) {
            return RecordStatus.HIGHER.getType();
        } else {
            return RecordStatus.NORMAL.getType();
        }
    }

    public static Integer getPercentageRate(int count, int total) {
        float result = (float) count * 100 / total;
        return (int) result;
    }

    /**
     * 计算 bmi
     *
     * @param height 身高
     * @param weight 体重
     * @return bmi
     */
    public static float getBmi(Float height, Float weight) {
        if (height == null || height == 0) {
            height = 165.0f;
        }
        if (weight == null || weight == 0) {
            weight = 55.0f;
        }
        float tmpBMI = weight / height / height * 10000;
        BigDecimal b = new BigDecimal(tmpBMI);
        return b.setScale(1, RoundingMode.HALF_UP).floatValue();
    }

    /**
     * 计算腰臀比
     *
     * @param waist 腰围
     * @param hip   臀围
     * @return 腰臀比
     */
    public static float getWhratio(Float waist, Float hip) {
        // 臀围为0直接返回0
        if (hip == 0L) {
            return 0L;
        }
        float tempWhratio = waist / hip;
        BigDecimal b = new BigDecimal(tempWhratio);
        return b.setScale(2, RoundingMode.HALF_UP).floatValue();
    }


}
